/************************************************************************************/
/*  Program name CAPAPIMP_INV_D.sql                                                 */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER AX_AP_INVOICE_DIST_ARDI1 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARDI2 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARU1 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARU2 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_BRU1 DISABLE;
--ALTER TRIGGER AX_AP_INVOICE_DIST_BRU2 DISABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T1 DISABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T2 DISABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T3 DISABLE;
ALTER TRIGGER JL_BR_AP_TAX_HOLDS DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_INV_D';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_INVOICES_DIST;

	lv_Next_Seq_ID	NUMBER;
	lv_SOB_Name		VARCHAR2(20);
	lv_COA_ID		NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_INVOICE_DISTRIBUTIONS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_INVOICES_DIST_MAP VALUES
			(
			C1.INVOICE_ID,
			C1.DISTRIBUTION_LINE_NUMBER,
			C1.INVOICE_DISTRIBUTION_ID,
			CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.INVOICE_ID),
			C1.DISTRIBUTION_LINE_NUMBER,
			lv_Next_Seq_ID
			);

			lv_SOB_Name	:=	CAPBRIMP_COMMON.GetSOBShortNameOU(CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME));
			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(lv_SOB_Name);

			INSERT INTO AP_INVOICE_DISTRIBUTIONS_ALL
			VALUES
			(
			C1.ACCOUNTING_DATE,
			C1.ACCRUAL_POSTED_FLAG,
			C1.ASSETS_ADDITION_FLAG,
			C1.ASSETS_TRACKING_FLAG,
			C1.CASH_POSTED_FLAG,
			C1.DISTRIBUTION_LINE_NUMBER,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.DIST_CCID_D),		--DIST_CODE_COMBINATION_ID,
			CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.INVOICE_ID),		--INVOICE_ID,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),			--LAST_UPDATED_BY,
			C1.LAST_UPDATE_DATE,
			C1.LINE_TYPE_LOOKUP_CODE,
			C1.PERIOD_NAME,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),				--SET_OF_BOOKS_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,CAPBREXT_COMMON.GetSegment(C1.ACCTS_PAY_CODE_COMBINATION_ID)),		-- ACCTS_PAY_CODE_COMBINATION_ID
			C1.AMOUNT,
			C1.BASE_AMOUNT,
			C1.BASE_INVOICE_PRICE_VARIANCE,
			CAPBRIMP_MAPPING.GetAPBatchIDMapping(C1.BATCH_ID),			--BATCH_ID,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),				--CREATED_BY,
			C1.CREATION_DATE,
			REPLACE(C1.DESCRIPTION,'^C10^',CHR(10)),
			C1.EXCHANGE_RATE_VARIANCE,
			C1.FINAL_MATCH_FLAG,
			C1.INCOME_TAX_REGION,
			C1.INVOICE_PRICE_VARIANCE,
			C1.LAST_UPDATE_LOGIN,
			C1.MATCH_STATUS_FLAG,
			C1.POSTED_FLAG,
			CAPBRIMP_MAPPING.GetPODistIDMapping(C1.PO_DISTRIBUTION_ID),	--PO_DISTRIBUTION_ID,
			C1.PROGRAM_APPLICATION_ID,
			C1.PROGRAM_ID,
			C1.PROGRAM_UPDATE_DATE,
			C1.QUANTITY_INVOICED,
			C1.RATE_VAR_CODE_COMBINATION_ID,
			C1.REQUEST_ID,
			C1.REVERSAL_FLAG,
			C1.TYPE_1099,
			C1.UNIT_PRICE,
			C1.VAT_CODE,
			C1.AMOUNT_ENCUMBERED,
			C1.BASE_AMOUNT_ENCUMBERED,
			C1.ENCUMBERED_FLAG,
			C1.EXCHANGE_DATE,
			C1.EXCHANGE_RATE,
			C1.EXCHANGE_RATE_TYPE,
			C1.PRICE_ADJUSTMENT_FLAG,
			C1.PRICE_VAR_CODE_COMBINATION_ID,
			C1.QUANTITY_UNENCUMBERED,
			C1.STAT_AMOUNT,
			C1.AMOUNT_TO_POST,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE_CATEGORY,
			C1.BASE_AMOUNT_TO_POST,
			C1.CASH_JE_BATCH_ID,		-- ???
			C1.EXPENDITURE_ITEM_DATE,
			C1.EXPENDITURE_ORGANIZATION_ID,
			C1.EXPENDITURE_TYPE,
			C1.JE_BATCH_ID,				-- ???
			CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.PARENT_INVOICE_ID),	--PARENT_INVOICE_ID,
			C1.PA_ADDITION_FLAG,
			C1.PA_QUANTITY,
			C1.POSTED_AMOUNT,
			C1.POSTED_BASE_AMOUNT,
			C1.PREPAY_AMOUNT_REMAINING,
			C1.PROJECT_ACCOUNTING_CONTEXT,
			C1.PROJECT_ID,				-- ???
			C1.TASK_ID,					-- ???
			C1.USSGL_TRANSACTION_CODE,
			C1.USSGL_TRX_CODE_CONTEXT,
			C1.EARLIEST_SETTLEMENT_DATE,
			C1.REQ_DISTRIBUTION_ID,		-- ???
			C1.QUANTITY_VARIANCE,
			C1.BASE_QUANTITY_VARIANCE,
			CAPBRIMP_MAPPING.GetBCPacketIDMapping(C1.PACKET_ID),		--PACKET_ID,
			C1.AWT_FLAG,
			C1.AWT_GROUP_ID,
			C1.AWT_TAX_RATE_ID,
			C1.AWT_GROSS_AMOUNT,
			C1.AWT_INVOICE_ID,
			C1.AWT_ORIGIN_GROUP_ID,
			C1.REFERENCE_1,
			C1.REFERENCE_2,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),						--ORG_ID,
			C1.OTHER_INVOICE_ID,
			C1.AWT_INVOICE_PAYMENT_ID,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.AMOUNT_INCLUDES_TAX_FLAG,
			C1.TAX_CALCULATED_FLAG,
			C1.LINE_GROUP_NUMBER,
			C1.RECEIPT_VERIFIED_FLAG,
			C1.RECEIPT_REQUIRED_FLAG,
			C1.RECEIPT_MISSING_FLAG,
			C1.JUSTIFICATION,
			C1.EXPENSE_GROUP,
			C1.START_EXPENSE_DATE,
			C1.END_EXPENSE_DATE,
			C1.RECEIPT_CURRENCY_CODE,
			C1.RECEIPT_CONVERSION_RATE,
			C1.RECEIPT_CURRENCY_AMOUNT,
			C1.DAILY_AMOUNT,
			C1.WEB_PARAMETER_ID,
			C1.ADJUSTMENT_REASON,
			C1.AWARD_ID,
			C1.MRC_DIST_CODE_COMBINATION_ID,
			C1.MRC_BASE_AMOUNT,
			C1.MRC_BASE_INV_PRICE_VARIANCE,
			C1.MRC_EXCHANGE_RATE_VARIANCE,
			C1.MRC_RATE_VAR_CCID,
			C1.MRC_EXCHANGE_DATE,
			C1.MRC_EXCHANGE_RATE,
			C1.MRC_EXCHANGE_RATE_TYPE,
			C1.MRC_RECEIPT_CONVERSION_RATE,
			C1.DIST_MATCH_TYPE,
			CAPBRIMP_COMMON.GetRCVTransID(C1.RCV_ORG_NAME,C1.RCV_REC_NUMBER,C1.RCV_QTY),	--RCV_TRANSACTION_ID,
			lv_Next_Seq_ID,									--INVOICE_DISTRIBUTION_ID,
				C1.PARENT_REVERSAL_ID,
			C1.TAX_RECOVERY_RATE,
			C1.TAX_RECOVERY_OVERRIDE_FLAG,
			C1.TAX_RECOVERABLE_FLAG,
			C1.TAX_CODE_OVERRIDE_FLAG,
			C1.TAX_CODE_ID,
			C1.PA_CC_AR_INVOICE_ID,
			C1.PA_CC_AR_INVOICE_LINE_NUM,
			C1.PA_CC_PROCESSED_CODE,
			C1.MERCHANT_DOCUMENT_NUMBER,
			C1.MERCHANT_NAME,
			C1.MERCHANT_REFERENCE,
			C1.MERCHANT_TAX_REG_NUMBER,
			C1.MERCHANT_TAXPAYER_ID,
			C1.COUNTRY_OF_SUPPLY,
			C1.MATCHED_UOM_LOOKUP_CODE,
			C1.GMS_BURDENABLE_RAW_COST,
				C1.ACCOUNTING_EVENT_ID,			--UPDATE WHEN ACCOUNTING EVENT AVAILABLE ?
				C1.PREPAY_DISTRIBUTION_ID,
			C1.CREDIT_CARD_TRX_ID,
			C1.UPGRADE_POSTED_AMT,
			C1.UPGRADE_BASE_POSTED_AMT,
			C1.INVENTORY_TRANSFER_STATUS,
			C1.COMPANY_PREPAID_INVOICE_ID,
			C1.CC_REVERSAL_FLAG,
			C1.PREPAY_TAX_PARENT_ID,
			C1.AWT_WITHHELD_AMT,
			C1.INVOICE_INCLUDES_PREPAY_FLAG,
			C1.PRICE_CORRECT_INV_ID,
			C1.PRICE_CORRECT_QTY,
			C1.PA_CMT_XFACE_FLAG,
			C1.CANCELLATION_FLAG,
			C1.FULLY_PAID_ACCTD_FLAG
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		UPDATE
			AP_INVOICE_DISTRIBUTIONS_ALL
		SET
			PARENT_REVERSAL_ID	=	CAPBRIMP_MAPPING.GetAPDistIDMapping(PARENT_REVERSAL_ID)
		WHERE	INVOICE_DISTRIBUTION_ID	IN
			(
				SELECT	INVOICE_DISTRIBUTION_ID_N
				FROM	CRST_AP_INVOICES_DIST_MAP
			)
		AND		PARENT_REVERSAL_ID		IS	NOT NULL;

		UPDATE
			AP_INVOICE_DISTRIBUTIONS_ALL
		SET
			PREPAY_DISTRIBUTION_ID	=	CAPBRIMP_MAPPING.GetAPDistIDMapping(PREPAY_DISTRIBUTION_ID)
		WHERE	INVOICE_DISTRIBUTION_ID	IN
			(
				SELECT	INVOICE_DISTRIBUTION_ID_N
				FROM	CRST_AP_INVOICES_DIST_MAP
			)
		AND		PREPAY_DISTRIBUTION_ID	IS	NOT NULL;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER AX_AP_INVOICE_DIST_ARDI1 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARDI2 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARU1 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARU2 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_BRU1 ENABLE;
--ALTER TRIGGER AX_AP_INVOICE_DIST_BRU2 ENABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T1 ENABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T2 ENABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T3 ENABLE;
ALTER TRIGGER JL_BR_AP_TAX_HOLDS ENABLE;
